let CommentDao = require('../dao/commentDao');
let TopicDao = require('../dao/topicDao');
const UserRightsDao = require('../dao/userRightsDao');
exports.create = function (params) {
    return new Promise(function (resolve, reject) {
        let insertCommentParams = {
            openid: params.openid,
            ToTopic: params.ToTopic,
            ToComment: params.ToComment,
            CommentContent: params.CommentContent,
            CreatedAt: new Date()
        };
        mysqlHelper.getConn()
            .then(function (conn) {
                UserRightsDao.queryRights(conn, params)
                    .then(function (result) {
                        if (result.length > 0 && result[0].RightsComment == 'T')
                            return conn.openTransaction();
                        throw {code: 401};
                    })
                    .then(function () {
                        return CommentDao.createComment(conn, insertCommentParams);
                    })
                    .then(function () {
                        return TopicDao.addTopicComment(conn, params);
                    })
                    .then(function (result) {
                        return conn.commitTransaction();
                    }, function (err) {
                        conn.rollbackTransaction();
                        throw err;
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve('评论成功')
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            });
    });
};
exports.deleteComment = function (params) {
    return new Promise(function (resolve, reject) {
        masterPool.query('select * from comment where CommentId = ?;',[params.CommentId],function (sErr,sResult) {
            console.log(this.sql)
            if(sErr){
                reject(sErr);
            }else{
                if(sResult.length===1){
                    if(sResult[0].openid === params.openid){
                        masterPool.query('delete from comment where CommentId = ?;',[params.CommentId],function (dErr,dResult) {
                            console.log(this.sql)
                            if(dErr){
                                reject(dErr)
                            }else{
                                masterPool.query('update topic set TopicCommentNum=TopicCommentNum-1 where TopicId=?;',[sResult[0].ToTopic],function (uErr,uResult) {
                                    if(uErr){
                                        reject(uErr)
                                    }else{
                                        resolve('删除成功');
                                    }
                                })
                            }
                        })
                    }else{
                        reject('非自己评论');
                    }
                }else{
                    reject('未找到评论')
                }
            }
        })
        // mysqlHelper.getConn()
        //     .then(function (conn) {
        //         doDelete(conn, [params.CommentId])
        //             .then(function () {
        //                 conn.releaseConn();
        //                 resolve('删除成功')
        //             }, function (err) {
        //                 console.log(err);
        //                 conn.releaseConn();
        //                 reject(err)
        //             });
        //     })
    })

    function doDelete(conn, idArr) {
        return selectFn(conn, idArr)
            .then(function (nextIdArr) {
                if (nextIdArr.length > 0) {
                    return deleteFn(conn, nextIdArr)
                        .then(function () {
                            return doDelete(conn, nextIdArr);
                        })
                }
            })
    }

    function deleteFn(conn, idArr) {
        return new Promise(function (resolve, reject) {
            conn.query('delete from comment where CommentId in(' + idArr.join(',') + ')', [], function (deleteE, deleteR) {
                if (deleteE) {
                    reject(deleteE)
                } else {
                    resolve(deleteR)
                }
            })
        })
    }

    function selectFn(conn, idArr) {
        return new Promise(function (resolve, reject) {
            let nextIdArr = [];
            conn.query('select CommentId from comment where ToComment in(' + idArr.join(',') + ')', [], function (err, result) {
                if (err) {
                    reject(err)
                } else {
                    for (let i in result) {
                        nextIdArr.push(result[i].CommentId);
                    }
                    resolve(nextIdArr)
                }
            })
        });
    }
}
exports.pcCommentList = function (params) {
    return new Promise(function (resolve, reject) {
        CommentDao.pcCommentList(masterPool, params, true)
            .then(resolve, reject);
    });
};
exports.pcDeleteComment = function (params) {
    return new Promise(function (resolve, reject) {
        TopicDao.reduceTopicComment(masterPool, params)
            .then(function () {
                return CommentDao.pcDeleteComment(masterPool, params, true);
            })
            .then(resolve("删除成功"), reject);
    });
};
